From 3f30f22d9d9c187e7ccb020cb538b18bdc971eae Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Fri, 14 Apr 2023 07:57:20 -0600 Subject: [PATCH] de-duplicate format and filter vector display code. (#1068) * de-duplicate format and filter vector display code. * consolidate release of argument memory in vecs, filter_vecs. --- filter_vecs.cc | 53 ++++++++------------------------------------------ filter_vecs.h | 5 ++--- main.cc | 20 +++++++++---------- vecs.cc | 43 +++++++++++++--------------------------- vecs.h | 4 ++-- 5 files changed, 36 insertions(+), 89 deletions(-) diff --git a/filter_vecs.cc b/filter_vecs.cc index 00308b71c..6e0f23d67 100644 --- a/filter_vecs.cc +++ b/filter_vecs.cc @@ -262,19 +262,9 @@ FilterVecs::fltinfo_t FilterVecs::find_filter_vec(const QString& fltargstring) return {}; } -void FilterVecs::free_filter_vec(fltinfo_t& filter) +void FilterVecs::free_filter_vec(Filter* flt) { - QVector* args = filter->get_args(); - - if (args && !args->isEmpty()) { - assert(args->isDetached()); - for (auto& arg : *args) { - if (arg.argvalptr) { - xfree(arg.argvalptr); - arg.argvalptr = *arg.argval = nullptr; - } - } - } + Vecs::free_options(flt->get_args()); } void FilterVecs::init_filter_vec(Filter* flt) @@ -300,16 +290,7 @@ void FilterVecs::init_filter_vecs() void FilterVecs::exit_filter_vec(Filter* flt) { (flt->exit)(); - QVector* args = flt->get_args(); - if (args && !args->isEmpty()) { - assert(args->isDetached()); - for (auto& arg : *args) { - if (arg.argvalptr) { - xfree(arg.argvalptr); - *arg.argval = arg.argvalptr = nullptr; - } - } - } + Vecs::free_options(flt->get_args()); } void FilterVecs::exit_filter_vecs() @@ -325,32 +306,14 @@ void FilterVecs::exit_filter_vecs() * Display the available formats in a format that's easy for humans to * parse for help on available command line options. */ -void FilterVecs::disp_filter_vecs() const -{ - for (const auto& vec : d_ptr_->filter_vec_list) { - Filter* flt = (vec.factory != nullptr)? vec.factory() : vec.vec; - printf(" %-20.20s %-50.50s\n", - qPrintable(vec.name), qPrintable(vec.desc)); - const QVector* args = flt->get_args(); - if (args) { - for (const auto& arg : *args) { - if (!(arg.argtype & ARGTYPE_HIDDEN)) { - printf(" %-18.18s %-.50s %s\n", - qPrintable(arg.argstring), qPrintable(arg.helpstring), - (arg.argtype & ARGTYPE_REQUIRED) ? "(required)" : ""); - } - } - } - if (vec.factory != nullptr) { - delete flt; - } - } -} - void FilterVecs::disp_filter_vec(const QString& vecname) const { for (const auto& vec : d_ptr_->filter_vec_list) { - if (vecname.compare(vec.name, Qt::CaseInsensitive) != 0) { + /* + * Display info for all filter is vecname is empty, + * otherwise just display info for the matching filter. + */ + if (!vecname.isEmpty() && (vecname.compare(vec.name, Qt::CaseInsensitive) != 0)) { continue; } Filter* flt = (vec.factory != nullptr)? vec.factory() : vec.vec; diff --git a/filter_vecs.h b/filter_vecs.h index aa5323b66..2abe2d366 100644 --- a/filter_vecs.h +++ b/filter_vecs.h @@ -67,13 +67,12 @@ public: static void prepare_filter(const fltinfo_t& fltdata); fltinfo_t find_filter_vec(const QString& fltargstring); - static void free_filter_vec(fltinfo_t& filter); + static void free_filter_vec(Filter* flt); static void init_filter_vec(Filter* flt); void init_filter_vecs(); static void exit_filter_vec(Filter* flt); void exit_filter_vecs(); - void disp_filter_vecs() const; - void disp_filter_vec(const QString& vecname) const; + void disp_filter_vec(const QString& vecname = QString()) const; void disp_filters(int version) const; bool validate_filters() const; diff --git a/main.cc b/main.cc index d3a65301c..6487ba604 100644 --- a/main.cc +++ b/main.cc @@ -109,7 +109,7 @@ load_args(const QString& filename, const QString& arg0) } static void -usage(const char* pname, int shorter) +usage(const char* pname, bool verbose) { printf("GPSBabel Version %s. https://www.gpsbabel.org\n\n", gpsbabel_version); @@ -149,14 +149,14 @@ usage(const char* pname, int shorter) , pname , global_opts.debug_level ); - if (shorter) { + if (!verbose) { printf("\n\n[Press enter]"); fgetc(stdin); } else { printf("File Types (-i and -o options):\n"); - Vecs::Instance().disp_vecs(); + Vecs::Instance().disp_vec(); printf("\nSupported data filters:\n"); - FilterVecs::Instance().disp_filter_vecs(); + FilterVecs::Instance().disp_filter_vec(); } } @@ -314,7 +314,7 @@ run(const char* prog_name) QStringList qargs = QCoreApplication::arguments(); if (qargs.size() < 2) { - usage(prog_name,1); + usage(prog_name, false); return 0; } @@ -346,7 +346,7 @@ run(const char* prog_name) if (argn < qargs.size()-1) { spec_usage(qargs.at(argn+1)); } else { - usage(prog_name,0); + usage(prog_name, true); } return 0; } @@ -459,7 +459,7 @@ run(const char* prog_name) filter->init(); filter->process(); filter->deinit(); - FilterVecs::free_filter_vec(filter); + FilterVecs::free_filter_vec(filter.flt); FilterVecs::exit_filter_vec(filter.flt); delete filter.flt; @@ -469,7 +469,7 @@ run(const char* prog_name) filter->init(); filter->process(); filter->deinit(); - FilterVecs::free_filter_vec(filter); + FilterVecs::free_filter_vec(filter.flt); } } else { fatal("Unknown filter '%s'\n",qPrintable(argument)); @@ -541,7 +541,7 @@ run(const char* prog_name) return 0; case 'h': case '?': - usage(prog_name,0); + usage(prog_name, true); return 0; case 'p': argument = FETCH_OPTARG; @@ -602,7 +602,7 @@ run(const char* prog_name) } } else if (!qargs.isEmpty()) { - usage(prog_name,0); + usage(prog_name, true); return 0; } if (!ovecs) { diff --git a/vecs.cc b/vecs.cc index d4a6fb0d1..24e36f474 100644 --- a/vecs.cc +++ b/vecs.cc @@ -660,10 +660,8 @@ bool Vecs::is_bool(const QString& val) (!val.isEmpty() && val.at(0).isDigit()); } -void Vecs::exit_vec(Format* fmt) +void Vecs::free_options(QVector* args) { - (fmt->exit)(); - QVector* args = fmt->get_args(); if (args && !args->isEmpty()) { assert(args->isDetached()); for (auto& arg : *args) { @@ -675,6 +673,12 @@ void Vecs::exit_vec(Format* fmt) } } +void Vecs::exit_vec(Format* fmt) +{ + (fmt->exit)(); + free_options(fmt->get_args()); +} + void Vecs::exit_vecs() { for (const auto& vec : d_ptr_->vec_list) { @@ -1040,39 +1044,20 @@ QVector Vecs::sort_and_unify_vecs() const return svp; } -#define VEC_FMT " %-20.20s %-.50s\n" - -void Vecs::disp_vecs() const -{ - const auto svp = sort_and_unify_vecs(); - for (const auto& vec : svp) { - if (vec.type == ff_type_internal) { - continue; - } - printf(VEC_FMT, qPrintable(vec.name), qPrintable(vec.desc)); - const QVector args = vec.arginfo; - for (const auto& arg : args) { - if (!(arg.argtype & ARGTYPE_HIDDEN)) { - printf(" %-18.18s %s%-.50s%s\n", - qPrintable(arg.argstring), - (arg.argtype & ARGTYPE_TYPEMASK) == - ARGTYPE_BOOL ? "(0/1) " : "", - qPrintable(arg.helpstring), - (arg.argtype & ARGTYPE_REQUIRED) ? " (required)" : ""); - } - } - } -} - void Vecs::disp_vec(const QString& vecname) const { const auto svp = sort_and_unify_vecs(); for (const auto& vec : svp) { - if (vecname.compare(vec.name, Qt::CaseInsensitive) != 0) { + /* + * Display info for all non-internal formats is vecname is empty, + * otherwise just display info for the matching format. + */ + if (vecname.isEmpty()? (vec.type == ff_type_internal) : + (vecname.compare(vec.name, Qt::CaseInsensitive) != 0)) { continue; } - printf(VEC_FMT, qPrintable(vec.name), qPrintable(vec.desc)); + printf(" %-20.20s %-.50s\n", qPrintable(vec.name), qPrintable(vec.desc)); const QVector args = vec.arginfo; for (const auto& arg : args) { if (!(arg.argtype & ARGTYPE_HIDDEN)) { diff --git a/vecs.h b/vecs.h index 9c585d432..e2ccf4829 100644 --- a/vecs.h +++ b/vecs.h @@ -73,6 +73,7 @@ public: static void init_vec(Format* fmt); void init_vecs(); + static void free_options(QVector* args); static void exit_vec(Format* fmt); void exit_vecs(); static void assign_option(const QString& module, arglist_t* arg, const QString& val); @@ -81,8 +82,7 @@ public: static QString get_option(const QStringList& options, const QString& argname); static void prepare_format(const fmtinfo_t& data); fmtinfo_t find_vec(const QString& fmtargstring); - void disp_vecs() const; - void disp_vec(const QString& vecname) const; + void disp_vec(const QString& vecname = QString()) const; static const char* name_option(uint32_t type); void disp_formats(int version) const; static bool validate_args(const QString& name, const QVector* args); -- 2.30.2